package com.wyp168.leetcode;

//import org.apache.tomcat.jni.Time;

import java.util.*;

/**
 * @ClassName UniqueLetterString
 * @Description TODO
 * @Author wyp168
 * @Date 2022/9/6 07:54
 */
public class UniqueLetterString {
    private static final int[] worldCount = new int[26];
    static {
        //对数组进行初始化
        Arrays.fill(worldCount, 0);
    }
    public static void main(String[] args) {

        UniqueLetterString uniqueLetterString = new UniqueLetterString();
//        long startTime = Time.now();
        String s = "MJYMRNGBCJUBGLTXGEJPICIKALQHJYWISCTXWFMAAAWNZIACGDWCXGYFPUZRQTAQOHJDBDMEOYZWRSREFEFHIXQVDRVRYNIFLMGSDDRIHHZLTWWHYYDBHZOBHMWYPGONRMYZVSPMFVMSTKMIZVJYDTDSFGCWEOMAECKRZLLZVCGQZGPJNUWCNQRDEDQCNEMIGRHISNNQOZQDVGEFINCTWIPMZVRPURGIQRUJTEOQDEVAGDIMXZTIXRHLPTGBFWKMGUFWJPEAOSZPIAHPYPQHGHCYVARQLTIQPCPNPOUEFXYXDMQATGCRJTOKADWCWUIGAVNANEBFXQCFTDVDRWZRETCFPMSIYMAPCTUGZNVWULWHREFLMFQZUIXJSXWCTGSOZYOJRBDOLSFEKORWXEFVLPKIKUEBJHUBLPUXCIGLMVSYSIWQCDXVXCLNXLFRVDYCDLTUTSOYXQRUGJNSXJUHPPQTCESCIIJIFSGMXBYWLPTWILLPRBAHIGRTWAFRTUQGPPAWCPTEROBWIQCJPVUAAUVYBVQWJFBVXAHRMFDGIBTWUJBNRGQMTLRLOXUKVRZIPPVXZCBJEKYHKCGGDYMVCZYNZDJVQURXNFAJMCLRRAXBYXFRRPKVCRYITAUMKVAOZULXWPTSFVJSYLLLVOSYLUMFJFPRVMPUDRMCWLIINEQNNJQRFFFOAPGRFUCXUSXHCVBBLANUNVSVMFZHAAXRZTCNUCDPIUHNODOMEEGLTOLYVAOQQPQJHUQLAFSJIGNMRNFFAFVNGWAMFVEUNCFVMVNXXWXDXOSNOHFWYBXDFMWNALQNBXAEVRRMYOAXZRNJHYWYFVSTNDSEWNLLHRTWSVXENKSCBIVIJFQNYSAMCKZOYFNAPUOTMDEXZKKRPMPPTTFICZERDNDSSUVEOMPQKEMTBWBODRHWSFPBMKAFPWYEDPCOWRUNTVYMXTYYEJQTAJKCJAKGHTDWMUYGECJNCXZCXEZGECRXONNSZMQM";
//        long endTime = Time.now();
        System.out.println(uniqueLetterString.uniqueLetterString(s));
//        System.out.println(endTime - startTime);
//        System.out.println(String.format("共耗时:%d秒", endTime - startTime));

    }

    public int uniqueLetterString(String s) {
        Map<String, Integer> subStringMap = new HashMap<>();
        int result = 0;
        for (int i = 1; i <= s.length(); i++) {
            int leftIndex = 0;
            while (leftIndex + i <= s.length()) {
                Set<String> keySet = subStringMap.keySet();
                String substring = s.substring(leftIndex, leftIndex + i);
                if (keySet.contains(substring)) {
                    result += subStringMap.get(substring);
                    leftIndex ++;
                    continue;
                }
                int tempResult = getUniqueSingleCount(substring);
                result += tempResult;
                leftIndex ++;
                subStringMap.put(substring, tempResult);
            }
        }
        return result;
    }

    public int getUniqueSingleCount(String s) {
        Set<String> strSet = new HashSet<>();
        int tempCount = 0;
        for (int i = 0; i < s.length(); i++) {
            char subChar = s.charAt(i);
            worldCount[subChar - 'A'] ++;
        }
        for (int i = 0; i < worldCount.length; i++) {
            if (worldCount[i] == 1)
                tempCount++;
        }
        //数组置空
        Arrays.fill(worldCount, 0);
        return tempCount;
    }


}
